﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;

namespace BluSteamClient.Model.Utility
{
    public class TcpNetwork
    {
        private static TcpClient _tcpClient;

        public static void Connect(string ip, int port)
        {
            _tcpClient = new TcpClient(ip, port);
        }

        public static void Send(string packet)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(packet);
            _tcpClient.GetStream().Write(bytes, 0, bytes.Length);
        }

        public static string Receive()
        {
            byte[] buffer = new byte[1024];
            int totalRead = 0;

            do
            {
                int read = _tcpClient.GetStream().Read(buffer, totalRead, buffer.Length - totalRead);
                totalRead += read;
            } while (_tcpClient.GetStream().DataAvailable);

            return Encoding.ASCII.GetString(buffer, 0, totalRead);
        }

        public static void Disconnect()
        {
            _tcpClient.GetStream().Close();
            _tcpClient.Close();
        }
    }
}
